<!DOCTYPE html>
<script src="../../../../resources/testharness.js"></script>
<script src="../../../../resources/testharnessreport.js"></script>
<script src="../../../../resources/gesture-util.js"></script>
<style>
  #vcontainer {
    position: absolute;
    left: 350px;
    top: 0px;
    width: 50px;
    height: 300px;
    overflow-y: scroll;
    overflow-x: hidden;
  }

  #hcontainer {
    position: absolute;
    left: 0px;
    top: 350px;
    width: 300px;
    height: 50px;
    overflow-y: hidden;
    overflow-x: scroll;

  }
  .space {
    background: linear-gradient(135deg, dodgerblue 0%, green 100%);
    width: 2000px;
    height: 2000px;
  }
</style>

<div id="vcontainer">
  <div class="space"></div>
</div>
<div id="hcontainer">
  <div class="space"></div>
</div>

<script>
  // This test originally guarded a regression in touchpad fling event
  // targeting, see https://crbug.com/423345. Since then, fling is no longer
  // processed in the renderer so this is less relevant but we still want to
  // try a touchpad fling. However, gpuBenchmarking.swipe doesn't support fling
  // on Mac touchpads because it generates a GestureFling event which Mac
  // doesn't process. Instead, Mac apps continue to receive wheel events from
  // the OS with momentum phase; gpuBenchmarking doesn't support generating
  // these kinds of events yet. Thus, on Mac, we'll settle for a simple
  // touchpad scroll.
  async function performGesture(test, start_point, direction) {
    const pixels_to_scroll = 50;
    if (navigator.platform.indexOf("Mac") != -1) {
      let delta;
      // Swipe direction is opposite to scroll direction since it assumes the
      // direction is of the finger.
      if (direction == "up") {
        delta = {x: 0, y: pixels_to_scroll};
      } else if (direction == "left") {
        delta = {x: pixels_to_scroll, y: 0};
      } else {
        throw new Error("Got an unexpected scroll direction");
      }
      await smoothScrollWithXY(delta.x,
                               delta.y,
                               start_point.x,
                               start_point.y,
                               GestureSourceType.TOUCHPAD_INPUT,
                               SPEED_INSTANT);
    } else {
      await swipe(pixels_to_scroll,
                  start_point.x,
                  start_point.y,
                  direction,
                  SPEED_INSTANT /*touch_velocity*/,
                  SPEED_INSTANT /*fling_velocity*/,
                  GestureSourceType.TOUCHPAD_INPUT);
    }
  }

  window.onload = async () => {
    const verticalContainer = document.getElementById('vcontainer');
    const horizontalContainer = document.getElementById('hcontainer');

    if (!window.internals) {
      assert_true(window.internals, "This test requires internals");
      return;
    }

    // Zoom and offset the visual viewport. This test ensures hit testing of
    // scrollers is correct in the presence of a visual viewport transform.
    internals.setPageScaleFactorLimits(1, 4.0);
    internals.setPageScaleFactor(2);
    internals.setVisualViewportOffset(200, 200);

    promise_test(async (test) => {
      const start_point = pointInElement(verticalContainer, 25, 295);
      // In a swipe gesture, the direction is the "finger direction", meaning
      // "up" scrolls the page down.
      await performGesture(test, start_point, "up");

      assert_greater_than(verticalContainer.scrollTop, 0);
    }, "Correctly targetted vertical container when zoomed");

    promise_test(async (test) => {
      const start_point = pointInElement(horizontalContainer, 295, 25);
      // In a swipe gesture, the direction is the "finger direction", meaning
      // "left" scrolls the page right.
      await performGesture(test, start_point, "left");

      assert_greater_than(horizontalContainer.scrollLeft, 0);
    }, "Correctly targetted horizontal container when zoomed");
  }
</script>
